import { h, render } from 'vue'
import hMessage from './Message.vue'

type Message = {
  type: 'success' | 'error' | 'warning',
  text: string,
  duration?: number
}

const div = document.createElement('div')
div.className = 'message-container'
document.body.appendChild(div)

let timer = -1
export default function Message(obj: Message) {
  const vNode = h(hMessage, {type: obj.type, text: obj.text})
  render(vNode, div)

  // 清除定时器
  clearTimeout(timer)
  // 开启器定时器
  timer = window.setTimeout(()=> {
    render(null, div)
  }, obj.duration)
}

Message.success = (val: string) => {
  Message({type: 'success', text: val, duration:5000})
}
Message.error = (val: string) => {
  Message({type: 'error', text: val, duration:5000})
}
Message.warning = (val: string) => {
  Message({type: 'warning', text: val, duration:5000})
}